package com.xy6.struct.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树节点
 * 
 * @author zhang
 * @since 2017-09-17
 * 
 * @param <T>
 */
public class BinaryTreeNode<T> {
	
	/**
	 * 值
	 */
	private T data;
	
	/**
	 * 父节点
	 */
	private BinaryTreeNode<T> parent;
	
	/**
	 * 子节点
	 */
	private List<BinaryTreeNode<T>> childList;

	private BinaryTreeNode<T> left;
	
	private BinaryTreeNode<T> right;
	
	public BinaryTreeNode(){
	}
	
	public BinaryTreeNode(T data){
		this.data = data;
	}
	
	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public BinaryTreeNode<T> getParent() {
		return parent;
	}

	public void setParent(BinaryTreeNode<T> parent) {
		this.parent = parent;
	}

	public List<BinaryTreeNode<T>> getChildList() {
		return childList;
	}

	public void setChildList(List<BinaryTreeNode<T>> childList) {
		this.childList = childList;
	}

	public BinaryTreeNode<T> getLeft() {
		return left;
	}

	public void setLeft(BinaryTreeNode<T> left) {
		this.left = left;
		if(null != left){
			left.setParent(this);
		}
		
		if(null == this.childList){
			this.childList = new ArrayList<>(2);
			childList.add(null);
			childList.add(null);
		}
		childList.set(0, left);
	}

	public BinaryTreeNode<T> getRight() {
		return right;
	}

	public void setRight(BinaryTreeNode<T> right) {
		this.right = right;
		if(null != right){
			right.setParent(this);
		}
		
		if(null == this.childList){
			this.childList = new ArrayList<>(2);
			childList.add(null);
			childList.add(null);
		}
		childList.set(1, right);
	}

	@Override
	public String toString() {
		return "BinaryTreeNode [data=" + data + ", parent=" + parent + ", left=" + left + ", right=" + right + "]";
	}

}
